#include <iostream>
#include <string>
#include <unordered_map>
#include <cstring>
using namespace std;

class User
{
public:
    User(const string& ip, const uint16_t port)
        :_ip(ip), _port(port)
    {}
    ~User()
    {}
    string getIp() const
    {
        return _ip;
    }

    uint16_t getPort() const
    {
        return _port;
    }
private:
    string _ip;
    uint16_t _port;
};

class OnlineUser
{
public:
    OnlineUser()
    {}
    void addUser(const string& ip, const uint16_t& port)
    {
        string name = ip + "-" + to_string(port);
        User user(ip, port);
        _online.insert(make_pair(move(name), move(user)));
    }
    void delUser(const string& ip, const uint16_t& port)
    {
        string name = ip + "-" + to_string(port);
        if(_online.count(name))
        {
            _online.erase(name);
        }
    }
    
    bool isOnline(const string& ip, const uint16_t& port)
    {
        string name = ip + "-" + to_string(port);
        return _online.count(name);
    }

    void broadCast(int sockfd, const string& ip, const uint16_t& port, const string& message)
    {
        for(const auto& e : _online)
        {
            struct sockaddr_in  client;
            bzero(&client, sizeof client);
            client.sin_family = AF_INET;
            client.sin_addr.s_addr = inet_addr(e.second.getIp().c_str());
            client.sin_port = htons(e.second.getPort());

            string response;
            response += ip + "-" + to_string(port) + "# " + message;
            sendto(sockfd, response.c_str(), response.size(), 0, (struct sockaddr*)&client, sizeof client);
        }
    }


    ~OnlineUser()
    {}
    
private:
    unordered_map<string, User> _online;
};